﻿Imports System.IO
Imports Ionic.Utils.Zip
Imports NewHeaderSystems.Archive.Core

Public Class FileArchiver
    ''' <summary>
    ''' Archives the confirm files, by zipping the files into a year sub folder with one zip file for each month.
    ''' </summary>
    ''' <param name="baseFilePath">The base file path.</param>
    ''' <param name="fileList">The file list.</param>
    ''' <param name="archiveFolderPath">The archive folder path.</param>
    Public Sub ArchiveConfirmFiles(ByVal baseFilePath As String, ByVal fileList() As String, ByVal archiveFolderPath As String)
        'Figure out the name of the zip file for this month and add the folder to the zip
        Dim zipFilePath = GetCurrentZipFilePath(archiveFolderPath)
        ZipHelper.AddFilesToZip(baseFilePath, fileList, zipFilePath, Now.ToString("yy.MM.dd-hh.mm.ss"))

        'Delete the temporary folder and the source files
        Delete(baseFilePath, fileList)
    End Sub

    ''' <summary>
    ''' Deletes the files that have been zipped.
    ''' </summary>
    ''' <param name="baseFilePath">The base file path.</param>
    ''' <param name="fileList">The file list.</param>
    Private Sub Delete(ByVal baseFilePath As String, ByVal fileList() As String)
        For Each sourceFile In fileList
            File.Delete(Path.Combine(baseFilePath, sourceFile))
        Next
    End Sub

    ''' <summary>
    ''' Determines the zip file path for the current month
    ''' </summary>
    ''' <param name="archiveFolderPath">The archive folder path.</param>
    ''' <returns></returns>
    Private Function GetCurrentZipFilePath(ByVal archiveFolderPath As String) As String
        Dim zipFilePath = Path.Combine(Now.ToString("yyyy"), Now.ToString("MM") + ".zip")
        Return Path.Combine(archiveFolderPath, zipFilePath)
    End Function
End Class
